/**
 * 说明：
 * 创建人：pluto
 * 创建时间：2018/8/3
 */
var ProductCtrl = function ($scope, ProductService, $uibModal) {
    $scope.param = {pageNum: 1, pageSize: 5};
    $scope.result = {};
    var _loadData = function (param) {
        ProductService.listForPage(param).then(function (res) {
            console.log(res);
            $scope.result = res;
        })
    };
    _loadData($scope.param);
    $scope.onPageChange = function () {
        $scope.param.pageNum = $scope.result.page.currentNo;
        _loadData($scope.param);
    };
    $scope.toEditor = function (data) {
        var modalInstance = $uibModal.open({
            templateUrl: 'pages/system/view/productedit.html',
            controller: 'ProductEditCtrl',
            backdrop: "true",
            size: 'md',
            resolve: {
                data: function () {
                    return data;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.param.pageNum = 1;
            _loadData($scope.param);
        }, function (aaa) {
        });
    }
};
var ProductEditCtrl = function ($scope, $uibModalInstance, data, ProductService) {
    $scope.product = angular.copy(data);
    $scope.uploadUrl = BASE_URL+'v0/file/upload';
    $scope.files = [];
    $scope.doSave = function () {
        $scope.product.icon = $scope.files.join(',');
        if($scope.product.id){
            ProductService.update($scope.product).then(function (res) {
                $uibModalInstance.close();
            }, function (error) {
                console.log(error);
            })
        }else{
            ProductService.save($scope.product).then(function (res) {
                $uibModalInstance.close();
            }, function (error) {
                console.log(error);
            })
        }

    };
    $scope.close = function () {
        $uibModalInstance.dismiss();
    }
};

angular.module('app.system.product.controller',[])
    .controller("ProductCtrl",ProductCtrl)
    .controller("ProductEditCtrl",ProductEditCtrl);